<template>
  <div id="mainone" style="width: 80%; height: 570px; margin: 0 auto"></div>
</template>

<script>
import * as echarts from "echarts";
export default {
  props: ["myStart", "myEnd"],
  data() {
    return {
      start: this.myStart,
      end: this.myEnd,
      num1: null,
      num2: null,

      rate1: null,
      rate2: null,
      rate3: null,
      rate4: null,
    };
  },
  created() {
    if (!this.start || !this.end) {
      this.axios({
        url: "grad/yearList",
        methods: "get",
      }).then((res) => {
        let years = res.data.data;

        years = years.sort(function (a, b) {
          return a - b;
        });

        //默认为最近一年
        this.start = years[years.length - 1];
        this.end = years[years.length - 1];
        this.get_dict(this.start, this.end);
      });
    } else {
      this.get_dict(this.start, this.end);
    }
  },
  computed: {
    myChart() {
      return echarts.init(document.getElementById("mainone"));
    },
  },
  methods: {
    get_dict(start, end) {
      this.axios({
        url: "grad/backToOrigin",
        method: "get",
        params: {
          start: start,
          end: end,
        },
        headers: {
          "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
        },
      }).then((res) => {
        for (let i = 0; i < res.data.data.length; i++) {
          let obj = res.data.data[i];
          if (obj.year == 0) {
            this.num1 = obj.number;
            this.rate1 = obj.rate;
            this.rate2 = 1 - this.rate1;
          } else {
            this.num2 = obj.number;
            this.rate3 = obj.rate;
            this.rate4 = 1 - this.rate3;
          }
        }
      });
      var title = ["非山东籍回原籍就业", "非山东籍不回原籍就业"];
      console.log(title);

      // 组织数据
    },
    drawChart() {
      echarts.init(document.getElementById("mainone")).dispose();
      this.myChart = echarts.init(document.getElementById("mainone"));
      // let numa=0;
      // let numb=0;
      // numa = this.num1*this.rate1;
      // numb = this.num1-numa;
      this.myChart.setOption({
        tooltip: {
          trigger: "item",
          // formatter: "{a} <br/>{b}: {c} ({d}%)",
        },

        legend: {
          orient: "vertical",
          left: "left",
        },

        series: [
          {},

          {
            name: "山东籍学生就业",
            type: "pie",
            radius: ["45%", "60%"],
            data: [
              { value: this.rate1, name: "留在本地" },
              { value: this.rate2, name: "不留在本地" },
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      });
    },
    resize() {
      this.myChart.resize();
    },
  },
};
</script>

<style scope></style>
